// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Debug Mode Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 9 : TYPE_DEBUGMODE

private alias object.value17	:	debugMode.currentSelection
private alias object.value18	:	player.currentPlane

// Function declarations
reserve function DebugMode_BlankSpawn
reserve function DebugMode_BlankDraw

// Static Values
public value DebugMode_ObjCount = 0;
public value DebugMode_ExitTimer = 0;

// Tables

// Large table to hold the types of every object
public table DebugMode_TypesTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

// Large table to hold the draw functions of every object
public table DebugMode_DrawTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

// Large table to hold the spawn functions of every object
public table DebugMode_SpawnTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function DebugMode_BlankSpawn
	temp0 = 0
end function


function DebugMode_BlankDraw
	DrawSprite(0)
end function

event ObjectMain
	AfterImagesCheck = 0
	if DebugMode_ExitTimer == 0
		if options.touchControls == true

			CheckTouchRect(0, 96, screen.xcenter, screen.ysize)

			if checkResult > -1
				arrayPos0 = checkResult
				temp0 = touchscreen[arrayPos0].xpos
				temp0 -= saveRAM[39]
				temp1 = touchscreen[arrayPos0].ypos
				temp1 -= saveRAM[40]
				ATan2(temp2, temp0, temp1)
				temp2 += 16
				temp2 &= 255
				temp2 >>= 5
				switch temp2
				case 0
					inputDown.right = true
					break

				case 1
					inputDown.right = true
					inputDown.down = true
					break

				case 2
					inputDown.down = true
					break

				case 3
					inputDown.down = true
					inputDown.left = true
					break

				case 4
					inputDown.left = true
					break

				case 5
					inputDown.left = true
					inputDown.up = true
					break

				case 6
					inputDown.up = true
					break

				case 7
					inputDown.up = true
					inputDown.right = true
					break

				end switch

			end if

			temp0 = screen.xcenter
			temp0 -= 48

			temp1 = temp0
			temp1 += 96

			CheckTouchRect(temp0, 0, temp1, 56)
			if checkResult > -1
				arrayPos0 = checkResult

				if touchscreen[arrayPos0].xpos > screen.xcenter
					if touchJump == false
						debugMode.currentSelection++
						if debugMode.currentSelection >= DebugMode_ObjCount
							debugMode.currentSelection = 0
						end if
					end if

					touchJump = true
				else
					if touchJump == false
						debugMode.currentSelection--

						if debugMode.currentSelection < 0
							debugMode.currentSelection += DebugMode_ObjCount
						end if
					end if
					touchJump = true
				end if
			else

				CheckTouchRect(screen.xcenter, 96, screen.xsize, 240)

				switch checkResult
				case -1
					touchJump = false
					break

				case 0
					inputDown.buttonC = true
					if touchJump == false
						inputPress.buttonC |= inputDown.buttonC
					end if
					touchJump = inputDown.buttonC
					break

				end switch
			end if

			temp0 = screen.xsize
			temp0 -= 88

			CheckTouchRect(temp0, 0, screen.xsize, 40)
			if checkResult > -1
				if options.stageSelectFlag == true

					// Start going to level select
					StopMusic()
					DebugMode_ExitTimer = 8
					object.priority = PRIORITY_ACTIVE_PAUSED
					options.touchControls = 0
					stage.state = 3

				else
					// Pause the game

					PlaySfx(SfxName[Pause ON], 0)
					StopSfx(SfxName[Flying])
					StopSfx(SfxName[Tired])
					foreach (TypeName[Pause Menu], arrayPos0, ALL_ENTITIES)
						object[arrayPos0].drawOrder = 6
						object[arrayPos0].state = 1
						object[arrayPos0].priority = PRIORITY_ACTIVE_PAUSED
					next

				end if
			end if

			if inputPress.start == true

					PlaySfx(SfxName[Pause ON], 0)
					StopSfx(SfxName[Flying])
					StopSfx(SfxName[Tired])
					foreach (TypeName[Pause Menu], arrayPos0, ALL_ENTITIES)
						object[arrayPos0].drawOrder = 6
						object[arrayPos0].state = 1
						object[arrayPos0].priority = PRIORITY_ACTIVE_PAUSED
					next

			end if
		end if

		CheckTouchRect(0, 0, 112, 56)
		
		if checkResult > -1
			inputDown.buttonZ = true
		end if

		if touchDebug == false
			inputPress.buttonZ |= inputDown.buttonB
		end if

		touchDebug = inputDown.buttonZ
		temp0 = false

		if inputDown.left == true
			object.xpos -= object.yvel
			temp0 = true
		end if

		if inputDown.right == true
			object.xpos += object.yvel
			temp0 = true
		end if

		if inputDown.up == true
			object.ypos -= object.yvel
			temp0 = true
		end if

		if inputDown.down == true
			object.ypos += object.yvel
			temp0 = true
		end if

		if temp0 == true
			object.yvel += 0xC00
			if object.yvel > 0x100000
				object.yvel = 0x100000
			end if
		else
			object.yvel = 0
		end if

		object.value3 = 0
		object.value4 = 0

		if inputPress.buttonZ == true
			if DebugMode_ExitTimer == 0

				// Exit Debug Mode
				object.type = TypeName[Player Object]
		
				object.speed = 0
				object.xvel = 0
				object.yvel = 0
				object.state = PlayerObject_HandleAir
				object.animation = ANI_WALKING
				object.animationSpeed = 0
				object.frame = 0
				object.controlMode = false
				object.drawOrder = -1
				player.currentPlane = 4
				object.interaction = true
				object.tileCollisions = true
				object.visible = true

			end if
		else
			if inputPress.buttonA == true
				// Go up one on the debug item list
				debugMode.currentSelection++
				if debugMode.currentSelection >= DebugMode_ObjCount
					debugMode.currentSelection = 0
				end if
			end if
			if inputDown.buttonA == true
				if inputPress.buttonC == true
					// Go down one on the debug item list
					debugMode.currentSelection--
					if debugMode.currentSelection < 0
						debugMode.currentSelection += DebugMode_ObjCount
					end if
				end if
			else
				if inputPress.buttonC == true
					// Place the object
					GetTableValue(temp0, debugMode.currentSelection, DebugMode_SpawnTable)
					CallFunction(temp0)
					object[tempObjectPos].priority = PRIORITY_XBOUNDS_DESTROY
				end if
			end if
		end if
	else

		options.touchControls = false

		DebugMode_ExitTimer += 8
		SetScreenFade(0, 0, 0, DebugMode_ExitTimer)

		if DebugMode_ExitTimer == 384

			// Load the level select
			starPostID = 0
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 5
			LoadStage()

		end if

	end if
end event


event ObjectDraw

	// Set this object to the selected type
	GetTableValue(object.type, debugMode.currentSelection, DebugMode_TypesTable)

	// Get the draw function of the selected object
	GetTableValue(temp0, debugMode.currentSelection, DebugMode_DrawTable)

	// Call said draw function
	CallFunction(temp0)

	// Go back to being a Debug Mode object
	object.type = TypeName[Debug Mode]

end event


event ObjectStartup

	LoadSpriteSheet("Global/Display.gif")

	// Appears as just a white square in this game, but it would be Sonic's (otherwise unused) CD life icon if using S1 2013's sheets
	SpriteFrame(-8, -8, 16, 16, 188, 239)

	DebugMode_ObjCount = 0

	// Fill the table wilt empty values
	// - Not needed on first run, but needed when refreshing a new scene in order to clear the table
	temp0 = 0
	while temp0 < 256
		SetTableValue(TypeName[DebugMode], temp0, DebugMode_TypesTable)
		SetTableValue(DebugMode_BlankDraw, temp0, DebugMode_DrawTable)
		SetTableValue(DebugMode_BlankSpawn, temp0, DebugMode_SpawnTable)
		temp0++
	loop

	DebugMode_ExitTimer = 0

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
